$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'orth/documentManage/list',
        datatype: "json",
        colModel: [
			{ label: '用户ID', name: 'id_', index: "id_", width: 35, key: true,hidden:true},
			{ label: '标题', name: 'title', width: 75 },
            { label: '内容', name: 'digest', width: 75},
            { label: '发表期刊', name: 'content', width: 45 },
            { label: '发表日期', name: 'publishTime', width: 35, formatter: function(value, options, row){
               return value!=undefined && value!=null && value!="" ? value.split("-")[0]+"-"+value.split("-")[1] : "";
            }},
            { label: '类型', name: 'directionType', width: 80, formatter: function(value, options, row){
                switch(value)
                {
                    case "1":return "关节修复与重建";
                    case "2":return "关节翻修";
                    case "3":return "下肢畸形矫正";
                    case "4":return "骨与关节感染";
                    case "5":return "风湿关节病";
                    default: return "";
                }
            }},
			{ label: '更新时间', name: 'updateTime', width: 80,sortable:false}
        ],
		viewrecords: true,
        
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
    $('.form-horizontal').validationEngine('attach',{
        autoHidePrompt: true,
        autoHideDelay: 2000,
        maxErrorsPerField: 1
    });
});

function uploadImage(id,name)
{
    $(id).fileupload({
        url: baseURL + "fileUpload/file",
        type: "POST",
        autoUpload: true,
        sequentialUploads: true
    }).bind('fileuploadprogress', function (e, data) {
        if(name=="attachment")
            vm.attachment = data.originalFiles[0].name;
    }).bind('fileuploaddone', function (e, data) {

        if(data.result.errcode==0){
            if(name=="attachment")
                vm.document.attachment = data.result.data;
            alert("上传成功！");
        }else{
            alert("上传失败！");
        }

    });
}

var vm = new Vue({
	el:'#rrapp',
	data:{
		q:{
			name: null
		},
		showList: true,
		title:null,
		document:{
			status:0,
			type:10,
			directionType:'1'
		},
        attachment:"",
        items:[{text:'关节修复与重建',value:'1'},{text:'关节翻修',value:'2'},{text:'下肢畸形矫正',value:'3'},{text:'骨与关节感染',value:'4'},{text:'风湿关节病',value:'5'}]
	},
	methods: {
		query: function () {
            jQuery("#jqGrid").jqGrid('setGridParam',{page:1});
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.attachment="";
			vm.document = {status:0,type:10,directionType:'1'};

		},
		update: function () {
			var userId = getSelectedRow();
			if(userId == null){
				return ;
			}
			
			vm.showList = false;
            vm.title = "修改";
			
			vm.getUser(userId);

		},
		del: function () {
			var userIds = getSelectedRows();
			if(userIds == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: baseURL + "orth/documentManage/delete",
                    contentType: "application/json",
				    data: JSON.stringify(userIds),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(){
                                vm.reload();
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		saveOrUpdate: function () {

            if(!$(".form-horizontal").validationEngine("validate")) {
                $(".formError").show();
                return;
            }
            var now=new Date();
            var publishTime=new Date(Date.parse(vm.document.publishTime));
            if(publishTime > now)
			{
				alert("发布时间不可大于今天");
				return;
			}

			var url = vm.document.id_ == null ? "orth/documentManage/save" : "orth/documentManage/update";
			$.ajax({
				type: "POST",
			    url: baseURL + url,
                contentType: "application/json",
			    data: JSON.stringify(vm.document),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});
		},
		getUser: function(id){
			$.get(baseURL + "orth/documentManage/info/"+id, function(r){
				vm.document = r.document;
				vm.attachment=vm.document.attachment;
                vm.document.publishTime=new Date(Date.parse(vm.document.publishTime));
			});
		},
		getRoleList: function(){
			$.get(baseURL + "sys/role/select", function(r){
				vm.roleList = r.list;
			});
		},
		reload: function () {
			vm.showList = true;
            $(".formError").hide();
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
                postData:{'title': vm.q.title},
                page:page
            }).trigger("reloadGrid");
		},
        validator: function () {

        }
	}
});